package com.zmy.xcx.controller;

import com.zmy.xcx.common.convention.result.Result;
import com.zmy.xcx.common.convention.result.Results;
import com.zmy.xcx.dao.entity.HyperglycemiaMedicationDO;
import com.zmy.xcx.dao.entity.HyperglycemiaSelfCheckDO;
import com.zmy.xcx.dto.HyperglycemiaMedicationDTO;
import com.zmy.xcx.dto.HyperglycemiaRiskAssessmentDTO;
import com.zmy.xcx.dto.HyperglycemiaRiskScoreDTO;
import com.zmy.xcx.dto.HyperglycemiaSelfCheckDTO;
import com.zmy.xcx.service.HyperglycemiaMedicationService;
import com.zmy.xcx.service.HyperglycemiaRiskAssessmentService;
import com.zmy.xcx.service.HyperglycemiaRiskScoreService;
import com.zmy.xcx.service.HyperglycemiaSelfCheckService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/hyperglycemia")
public class HyperglycemiaController {

    private final HyperglycemiaRiskAssessmentService hyperglycemiaRiskAssessmentService;
    private final HyperglycemiaRiskScoreService hyperglycemiaRiskScoreService;
    private final HyperglycemiaSelfCheckService hyperglycemiaSelfCheckService;
    private final HyperglycemiaMedicationService hyperglycemiaMedicationService;

    @PostMapping("/risk/assessment")
    public Result<Void> riskAssessment(@RequestBody HyperglycemiaRiskAssessmentDTO hyperglycemiaRiskAssessmentDTO) {
        hyperglycemiaRiskAssessmentService.addRiskAssessmentInfo(hyperglycemiaRiskAssessmentDTO);
        return Results.success();
    }

    @PostMapping("/risk/score")
    public Result<Void> riskScore(@RequestBody HyperglycemiaRiskScoreDTO hyperglycemiaRiskScoreDTO) {
        hyperglycemiaRiskScoreService.addRiskScoreInfo(hyperglycemiaRiskScoreDTO);
        return Results.success();
    }

    @PostMapping("/self-check")
    public Result<Void> createSelfCheckInfo(@RequestBody HyperglycemiaSelfCheckDTO hyperglycemiaSelfCheckDTO) {
        hyperglycemiaSelfCheckService.addHyperglycemiaSelfCheckInfo(hyperglycemiaSelfCheckDTO);
        return Results.success();
    }

    @GetMapping("/history")
    public Result<List<HyperglycemiaSelfCheckDO>> listSelfCheckInfo(@RequestHeader("openid") Integer openid) {
        List<HyperglycemiaSelfCheckDO> selfCheckInfos = hyperglycemiaSelfCheckService.listSelfCheckInfo(openid);
        return Results.success(selfCheckInfos);
    }

    @GetMapping("/medication")
    public Result<List<HyperglycemiaMedicationDO>> getHyperglycemiaMedication(
            @RequestParam(required = false) String drugName,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Boolean isRx,
            @RequestParam(required = false) String source) {
        HyperglycemiaMedicationDTO dto = new HyperglycemiaMedicationDTO();
        dto.setDrugName(drugName);
        dto.setType(type);
        dto.setIsRx(isRx);
        dto.setSource(source);
        List<HyperglycemiaMedicationDO> hyperglycemiaMedications = hyperglycemiaMedicationService.listHyperglycemiaMedication(dto);
        return Results.success(hyperglycemiaMedications);
    }

    @PostMapping("/medication/clicks/{id}")
    public void incrementPageView(@PathVariable("id") Integer id) {
        hyperglycemiaMedicationService.incrementPageView(id);
    }

}
